/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package firstround09;

import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author robit
 */
public class DecisionTree{
    /*
      All neccesary to be defined
      including:
      variable, inner class
    */
    // static int A;
    // static void B();
    // static class C
    static Pattern p;
    static Matcher m;
    /*
       One calculation fits all of the cases.
    */
    static void preReckon(){
        p=Pattern.compile("\\(|\\)|\\d.\\d+|[\\w&&\\D]+");
        return;
    }
/*
(0.2 furry
  (0.81 fast
    (0.3)
    (0.2)
  )
  (0.1 fishy
    (0.3 freshwater
      (0.01)
      (0.01)
    )
    (0.1)
  )
)
 */
    static void collect(HashMap node){
//  (0.1 fishy
//    (0.3 freshwater
//      (0.01)
//      (0.01)
//    )
//    (0.1)
//  )

        if(m.find()){/* ( */}

        if(m.find()){
           node.put("VALUE", m.group());
        }

        if(m.find()){
            if(m.group().matches("[\\w&&\\D]+")){
                node.put("FEATURE", m.group());
                HashMap yes=new HashMap();
                collect(yes);
                HashMap no=new HashMap();
                collect(no);
                node.put("YES",yes);
                node.put("NO",no);
                if(m.find()){/* ) */ return;}
            }else if(m.group().matches("\\)")){ return; }
        }        
    }

    /*
      calculate the answer
    */
    private static void reckon(int line) {
        int L=sc.nextInt();sc.nextLine();
        StringBuilder buffer=new StringBuilder();
        for(int i=0;i<L;i++){
            buffer.append(sc.nextLine());
        }
        int A=sc.nextInt();
        HashSet<String> feats[]=new HashSet[A];
        for(int i=0;i<A;i++){
            sc.next();
            int n=sc.nextInt();
            feats[i]=new HashSet<String>();
            for(int j=0;j<n;j++){
                feats[i].add(sc.next());
            }
        }

        //Arrays.sort(P);
        out.println(String.format("Case #%d: ",line));

        HashMap root=new HashMap();
        m=p.matcher(buffer);
        collect(root);


        for(int i=0;i<A;i++){
            double ans=1.0;
            HashMap itr=root;
            ans*=Double.parseDouble((String)itr.get("VALUE"));
            while(itr.containsKey("FEATURE")){
                if(feats[i].contains((String)itr.get("FEATURE"))){
                    itr=(HashMap)itr.get("YES");
                }else{
                    itr=(HashMap)itr.get("NO");
                }
                ans*=Double.parseDouble((String)itr.get("VALUE"));
            }
            out.println(ans);
            sys.println("subcase pass");
        }

        sys.println("case pass");
        //anl.println(String.format("N=%d K=%d\n%s", N, K, ans));
        //println(output);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*
           Pre-computing for all cases
        */

        // Just in case!
        preReckon();
        //while((char)read()!='P');

        /*
          Input-output
        */
        boolean test=false;
        boolean small=false;
        String prob="A";
        String filename;
        if(test) filename="test";
        else{
            if(small) filename=prob+"-small-practice";
            else filename=prob+"-large-practice";
        }
        try {
            /*
               Init IO,plus determination of input line number
             */
            initFileIO(filename);
            int lineNums=sc.nextInt();
            for (int i = 1; i <= lineNums; i++) {
                sys.println("Case"+i);
                reckon(i);                
            }
        } finally {
            endIO();
        }
    }

    private static Scanner sc = null;
    private static PrintWriter out,anl = null;
    private static PrintStream sys=System.out;
    public final static int OUTFILE=0,ANALFILE=1;

    public static void  initFileIO(String filename) throws IOException{
        sc = new Scanner(
                new BufferedReader(new FileReader(filename+".in")));
            //s.useLocale(Locale.US);
        out = new PrintWriter(
                new BufferedWriter(new FileWriter(filename+".out")));
        anl = new PrintWriter(
                new BufferedWriter(new FileWriter(filename+".anal")));

    }

    public static void endIO(){
        sc.close();
        out.close();
    }

}
